﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using Castle.Windsor;
using System.Web.Mvc;
using System.Reflection;
using Castle.MicroKernel.Registration;
using MvcContrib.Castle;
using MShop.Core.Util;
using Castle.DynamicProxy;
using MShop.Core.DataAccess;
using MShop.Web.Components.ModelBinders;
 using MShop.Core.HandleSelector;
using Castle.Core;
using Microsoft.Practices.ServiceLocation;
using MShop.Core.Windsor;
using MShop.Core.Service.Context;
using MShop.Core.Service.Membership;
using MShop.Core;
using MShop.Core.Service.Search;
using MShop.Core.Service.EmailSender;
 


namespace MShop.Web.Components
{
    public static class Bootstrapper
    {
       

        public static void InitializeContainer(IWindsorContainer container)
        {
                IoC.Initialize(container); 

                // register handler selectors
                container.Kernel.AddHandlerSelector(new UrlBasedComponentSelector(
                                                        typeof(IConnectionStringProvider)
                                                        ));
               
                container.Register(
                   Component.For<IConnectionStringProvider>().ImplementedBy<ConnectionStringProvider>().LifeStyle.Is(LifestyleType.Transient),
                   Component.For<IObjectContextProvider>().ImplementedBy<ObjectContextProvider>().LifeStyle.PerWebRequest,
                   Component.For(typeof(ICommonDao<>)).ImplementedBy(typeof(CommonDao<>)).LifeStyle.Transient,
                   Component.For(typeof(System.Web.Mvc.DefaultModelBinder)).ImplementedBy(typeof(DefaultModelBinder)).LifeStyle.Transient,
                   Component.For(typeof(PayModelBinder)).ImplementedBy(typeof(PayModelBinder)).LifeStyle.Transient,
                   Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)),
                   Component.For(typeof(MShop.Core.Service.IRecursionSort<>)).ImplementedBy(typeof(MShop.Core.Service.RecursionSortService<>)).LifeStyle.Transient,
                   Component.For<ICommonResolver>().ImplementedBy<CommonResolver>().LifeStyle.Transient,
                   Component.For<IShopContextService>().ImplementedBy<ShopContextService>().LifeStyle.Transient,
                   Component.For<IShopContext>().ImplementedBy<ShopContext>().LifeStyle.Transient,
                   Component.For<IUserRightsService>().ImplementedBy<DefaultUserRightsService>().LifeStyle.Transient,
                   Component.For<IRoleService>().ImplementedBy<DefaultRoleService>().LifeStyle.Transient,
                   Component.For(typeof(ProductBinder)).ImplementedBy(typeof(ProductBinder)).LifeStyle.Is(LifestyleType.Transient),
                   Component.For(typeof(IContentItemService<>)).ImplementedBy(typeof(SearchDecorator<>)).LifeStyle.Transient,
                   Component.For<ISearchService>().ImplementedBy<SearchService>().LifeStyle.Transient,
                   Component.For<IEmailSender>().ImplementedBy<DefaultEmailSender>().LifeStyle.Transient
                 );

                container.Register(AllTypes.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IController>().Configure(c => c.LifeStyle.Transient));
                   

                ServiceLocator.SetLocatorProvider(() => container.Resolve<IServiceLocator>());

                ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
           
         
        }
 
        
    }
}
